﻿//    Copyright 2014 LuoZhihui
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//      http://www.apache.org/licenses/LICENSE-2.0
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.

#region  导入名称空间

using System;
using Autofac;
using Autofac.Configuration;
using LotteryEngine.Infrastructure.CommonServiceLocator;
using Microsoft.Practices.ServiceLocation;

#endregion

namespace LotteryEngine.BootStrapper
{
	/// <summary>
	/// 抽奖引擎引导程序
	/// </summary>
	public class Bootstrap
	{
		[ThreadStatic] private static readonly object syncLocker;
		[ThreadStatic] private static IServiceLocator _serviceLocator;


		static Bootstrap()
		{
			syncLocker = new object();
		}

		private Bootstrap()
		{
		}

		public static IServiceLocator ServiceLocator
		{
			get
			{
				if (_serviceLocator == null)
				{
					lock (syncLocker)
					{
						if (_serviceLocator == null)
						{
							Init();
						}
					}
				}

				return _serviceLocator;
			}
		}

		[STAThread]
		private static void Init()
		{
			var builder = new ContainerBuilder();
			builder.RegisterModule(new ConfigurationSettingsReader("autofac"));
			var container = builder.Build();
			_serviceLocator = new AutofacServiceLocator(container);
		}
	}
}